package com.lagou.config;

import com.lagou.filter.LimitFilter;
import com.lagou.filter.LoginFilter;
import com.lagou.filter.TokenFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RouteLocatorConfig {

    @Value("${capacity}")
    Integer capacity;
    @Value("${duration}")
    Integer duration;


    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        System.out.println("============================RouteLocatorConfig "+ builder.routes());
        return builder
                .routes()
                .route(r -> r.path("/api/user/register/**")
                        .filters(f -> f.stripPrefix(1).filter(new LimitFilter(capacity,duration)))
                        .uri("lb://lagou-service-user")
                        .order(0)
                        .id("service-user-router"))

                .route(r -> r.path("/api/user/login/**")
                        .filters(f -> f.stripPrefix(1).filter(new LoginFilter()))
                        .uri("lb://lagou-service-user")
                        .order(0)
                        .id("service-user-router"))

                .route(r -> r.path("/api/user/info/**")
                        .filters(f -> f.stripPrefix(1).filter(new TokenFilter()))
                        .uri("lb://lagou-service-user")
                        .order(0)
                        .id("service-user-router"))
                .build();
    }
}
